package com.liuyun.util;

import java.security.SecureRandom;

/**
 * <p>
 * Copyright: (C), 2021/1/28 22:23
 * <p>
 * <p>
 * Company Information & Data Security Solutions Co., Ltd.
 *
 * @author LiuYun liujw@idss-cn.com
 * @version 1.0
 */
public class StringUtil {
    private StringUtil() {
    }

    /**
     * 选取可用的字符，用作产生字符串，此处就简单的使用了Base64的字符及
     */
    private static final char[] CHAR_POOL = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/".toCharArray();

    /**
     * 字符集大小
     */
    public static final int CHAR_POOL_SIZE = CHAR_POOL.length;

    /**
     * 随机源
     */
    private static SecureRandom secureRandom = new SecureRandom();

    /**
     * 获取指定长度的随机字符串
     *
     * @param length
     * @return
     */
    public static String buildString(int length) {
        char[] chars = buildCharArray(length);
        return new String(chars);
    }

    /**
     * 构建指定长度的字符数组
     *
     * @param length
     * @return
     */
    public static char[] buildCharArray(int length) {
        char[] chars = new char[length];
        for (int i = 0; i < length; i++) {
            chars[i] = CHAR_POOL[secureRandom.nextInt(CHAR_POOL_SIZE)];
        }
        return chars;
    }
}
